/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vn.webapp.dao.ClassesDAO;
import vn.webapp.dao.ClassingDAO;
import vn.webapp.dao.CoursingDAO;
import vn.webapp.dto.DataPage;
import vn.webapp.model.Classes;
import vn.webapp.model.Classing;
import vn.webapp.model.CourseTeacherClassRaw;
import vn.webapp.model.Coursing;
import vn.webapp.model.CoursingsSave;

@Service("coursingService")
public class CoursingServiceImpl implements CoursingService {

    @Autowired
    private CoursingDAO coursingDAO;
    @Autowired
    private ClassingDAO classingDAO;
    @Autowired
    private ClassesDAO classesDAO;
    
    public void setCoursingDAO(CoursingDAO coursingDAO) {
        this.coursingDAO = coursingDAO;
    }

    public void setClassingDAO(ClassingDAO classingDAO) {
        this.classingDAO = classingDAO;
    }

    public void setClassesDAO(ClassesDAO classesDAO) {
        this.classesDAO = classesDAO;
    }
    
    @Override
    @Transactional
    public DataPage<Coursing> list() {
        DataPage<Coursing> dataCoursing = new DataPage<>();
        dataCoursing.setData(coursingDAO.list());
        
        /* Get all courseteacher id */
        List<List<Classing>> aClassings = new ArrayList<>();
        //List<Classes> classeses = classesDAO.getByIds(cids);
        
        for(Coursing c: dataCoursing.getData()){
            aClassings.add(c.getClassing());
            List<Integer> classIds = new ArrayList<>();
            for(Classing cls : c.getClassing())
            {
                classIds.add(cls.getClass_id());
            }
            List<Classes> classeses = classesDAO.getByIds(classIds);
            c.setClasses(classeses);
        }
        /* Get all class id by courseteacher id from the table courseteacherclass */
//        List<Classing> classings = classingDAO.list() ;
//        List<Integer> cids = new ArrayList<>();
//        for(Classing classing: classings){
//            cids.add(classing.getClassid());
//        }
        
        /* Get all class info by class id from the table classes */
//        List<Classes> classeses = classesDAO.getByIds(cids);
//        for(Coursing c: dataCoursing.getData()){
//           for(Classing classing: classings){
////               for(Classes classes: classeses){
////                   if(c.getId()==classing.getCourseteacherid()){
////                       c.setClasses(classes);
////                   }
////               }
//           }       
//        }
        return dataCoursing;
    }
    
    @Override
    public int removeACoursing(int id){
        List<Classing> classings = classingDAO.getById(id);
        if(classings != null)
        {
            for(Classing c : classings)
            {
                classingDAO.removeAClassing(c.getCourseteacherclass_id());
            }
        }
        return coursingDAO.removeACoursing(id);
    }
    
    @Override
    public Coursing checkExists(int id, int teacherId, int courseId, int sessionId){
        return coursingDAO.checkExists(id, teacherId, courseId, sessionId);
    }
    
    @Override
    public int saveACoursing(int teacherId, int courseId, int sessionId){
        
        CoursingsSave coursing = new CoursingsSave();
        coursing.setTeacher_id(teacherId);
        coursing.setCourse_id(courseId);
        coursing.setSession_id(sessionId);
        
        return coursingDAO.saveACoursing(coursing);
    }

    @Override
    public void editACoursing(int id, int teacherId, int courseId, int sessionId, int[] classIds){
        
        CoursingsSave coursing = new CoursingsSave();
        coursing.setCourseteacher_id(id);
        coursing.setTeacher_id(teacherId);
        coursing.setCourse_id(courseId);
        coursing.setSession_id(sessionId);
        
        coursingDAO.editACoursing(coursing);
        List<Classing> classings = classingDAO.getById(id);
        if(classings != null)
        {
            for (Classing classing : classings) {
                classingDAO.removeAClassing(classing.getCourseteacherclass_id());
            }
        }
        Classing classing = new Classing();
        classing.setCourseteacher_id(id);
        for (int classId : classIds) {
            classing.setClass_id(classId);
            classingDAO.saveAClassing(classing);
        }
        
    }
    
    @Override
    @Transactional
    public DataPage<Coursing> filter(int page, int pageSize) {

        DataPage<Coursing> dataCoursing = new DataPage<>();
        dataCoursing.setPageIndex(page);
        dataCoursing.setPageSize(pageSize); 
        dataCoursing.setDataCount(coursingDAO.count());
        dataCoursing.setPageCount(dataCoursing.getDataCount()/pageSize);
        if(dataCoursing.getDataCount()%pageSize > 0){
            dataCoursing.setPageCount(dataCoursing.getPageCount()+1);
        }
        
        List<Coursing> coursing = coursingDAO.filter(page, pageSize);
        dataCoursing.setData(coursing);
        
        //List<Classes> classeses = classesDAO.getByIds(cids);
        
        for(Coursing c: dataCoursing.getData()){
            
            List<Integer> classIds = new ArrayList<>();
            for(Classing cls : c.getClassing())
            {
                classIds.add(cls.getClass_id());
            }
            List<Classes> classeses = classesDAO.getByIds(classIds);
            c.setClasses(classeses);
        }
        
        return dataCoursing;
    }
    
    @Override
    public Set<Coursing> getById(int coursingId)
    {
        List<Coursing> coursing = coursingDAO.getById(coursingId);
        Set<Coursing> course = new HashSet<Coursing>(coursing);
        return course;
    }
    
    
    @Override
    public List<CoursingsSave> getOriginSource()
    {
        return coursingDAO.getOriginSource();
    }
    
    @Override
    public List<CourseTeacherClassRaw> listCourseTeacherClassById(int iCourseTeacherClassId)
    {
        
        List<CourseTeacherClassRaw> listCourseTeacherClassById = coursingDAO.listCourseTeacherClassById(iCourseTeacherClassId);
        return listCourseTeacherClassById;
    }
}
